using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;

namespace DataSender
{
    public partial class Form1 : Form
    {
        
        int Sec = 0;
        Thread readThread;
        public Form1()
        {
            InitializeComponent();
            if (!serialPort1.IsOpen)
            {
                serialPort1.Open();
            }
//             if (!serialPort2.IsOpen)
//             {
//                 serialPort2.Open();
//             }
            readThread = new Thread(TProc);
        }
        //01 03 08 00 00 00 00 00 00 00 00 00 17 6F
        private void button1_Click(object sender, EventArgs e)
        {
            byte[] by = new byte[6];
            byte[] send = new byte[8];
            send[0] = by[0] = Convert.ToByte("09", 16);
            send[1] = by[1] = Convert.ToByte("03", 16);
            send[2] = by[2] = Convert.ToByte("00", 16);
            send[3] = by[3] = Convert.ToByte("20", 16);
            send[4] = by[4] = Convert.ToByte("00", 16);
            send[5] = by[5] = Convert.ToByte("02", 16);
            CCRC CRC = new CCRC();
            CRC.CalculateCrc16(send,out send[6],out send[7]);
//            MessageBox.Show(CRC.CalculateCrc16(by).ToString("X"));
            MessageBox.Show(send[6].ToString("X") + " " + send[7].ToString("X"));
            serialPort1.Write(send, 0, 8);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
            }
            readThread.Abort();
        }


        private void button2_Click(object sender, EventArgs e)
        {
//             if (!timer1.Enabled)
//             {
//                 timer1.Start();
//                 button2.Text = "Stop";
//             }
//             else
//             {
//                 timer1.Stop();
//                 button2.Text = "read";
//             }
//             if (!readThread.IsAlive)
//             {
//                 readThread.Start();
//                 button2.Text = "stop";
//             }
//             else
//             {
//                 readThread.Suspend();
//                 button2.Text = "read";
//             }
            readThread.Start();
        }
        private void TProc()
        {
            while (true)
            {
                byte[] recv = new byte[32];
                int len = serialPort1.Read(recv, 0, 8);
                String str = len.ToString() + " received : ";
                if (len > 0)
                {
                    for (int i = 0; i < len; i++)
                    {
                        str += Convert.ToString(recv[i],16) + " ";
                    }
                }
                else
                {
                    str += "null";
                }
                textBox1.Text = str;
                Sec += 1;
                textBox2.Text = Sec.ToString();
                Thread.Sleep(1000);
            }
        }
        //01 03 08 00 00 00 00 00 00 00 00 00 17 6F
        private void button3_Click(object sender, EventArgs e)
        {
            byte[] send = new byte[14];
            send[0] = Convert.ToByte("01", 16);
            send[1] = Convert.ToByte("03", 16);
            send[2] = Convert.ToByte("08", 16);
            send[3] = Convert.ToByte("00", 16);
            send[4] = Convert.ToByte("00", 16);
            send[5] = Convert.ToByte("00", 16);
            send[6] = Convert.ToByte("00", 16);
            send[7] = Convert.ToByte("00", 16);
            send[8] = Convert.ToByte("00", 16);
            send[9] = Convert.ToByte("00", 16);
            send[10] = Convert.ToByte("00", 16);
            send[11] = Convert.ToByte("00", 16);
            send[12] = Convert.ToByte("17", 16);
            send[13] = Convert.ToByte("6F", 16);
            CCRC CRC = new CCRC();
            CRC.CalculateCrc16(send, out send[12], out send[13]);
            //            MessageBox.Show(CRC.CalculateCrc16(by).ToString("X"));
            MessageBox.Show(send[12].ToString("X") + " " + send[13].ToString("X"));
        }
    }
}